//
//  CTRSAUtil.h
//  CTOpenSSL
//
//  Created by Andney Jack on 2021/2/22.
//

#import <Foundation/Foundation.h>

typedef NS_ENUM (NSUInteger, RSAKeyType) {
    RSAKeyTypePublic,
    RSAKeyTypePrivate,
};

@interface CTRSAUtil : NSObject

- (instancetype)initWithRSA2:(BOOL)rsa2;

// 导入key
- (BOOL)importKey:(NSString *)key keyType:(RSAKeyType)keyType;
- (BOOL)importKeyWithPath:(NSString *)path keyType:(RSAKeyType)keyType;

/// 验证签名 Sha1 + RSA
- (BOOL)verifyString:(NSString *)string withSign:(NSString *)sign keyType:(RSAKeyType)keyType;
/// 验证签名 md5 + RSA
- (BOOL)verifyMD5String:(NSString *)string withSign:(NSString *)sign keyType:(RSAKeyType)keyType;

- (NSString *)signString:(NSString *)string keyType:(RSAKeyType)keyType; // urlEncoding为NO

/// RAS签名
/// @param string 原始文本
/// @param keyType 密钥类型
/// @param urlEncoding 是否对结果进行URL编码
- (NSString *)signString:(NSString *)string keyType:(RSAKeyType)keyType urlEncoding:(BOOL)urlEncoding;
- (NSString *)signMD5String:(NSString *)string keyType:(RSAKeyType)keyType;

/// RAS加密
/// @param content 原始文本
/// @param keyType 密钥类型
/// @param urlEncoding 是否先对文本进行URL编码（解决中文问题）
- (NSString *)encode:(NSString *)content keyType:(RSAKeyType)keyType urlEncoding:(BOOL)urlEncoding;

/// RAS解密
/// @param content 原始文本
/// @param keyType 密钥类型
/// @param urlDecoding 是否对解密后的文本进行URL解码
- (NSString *)decode:(NSString *)content keyType:(RSAKeyType)keyType urlDecoding:(BOOL)urlDecoding;

/// 长文本RAS加密
- (NSString *)encodeLongText:(NSString *)content keyType:(RSAKeyType)keyType urlEncoding:(BOOL)urlEncoding;
/// 长文本RAS解密（有问题，待解决）
- (NSString *)decodeLongText:(NSString *)content keyType:(RSAKeyType)keyType urlDecoding:(BOOL)urlDecoding;

+ (NSString *)formatRSAKey:(NSString *)key keyType:(RSAKeyType)keyType; // 格式化key
+ (NSString *)urlEncode:(NSString *)text; // 字符串URL编码
+ (NSString *)urlDecode:(NSString *)text; // 字符串URL解码

@end
